package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class RegisterPage extends Utils {
    private static String url = "http://47.108.48.236:8081/register.html";
    public RegisterPage() {
        super(url);
    }

    /**
     * 检查页面是否加载成功
     */
    public void registerPageRight() {
        // 查看页面元素是否存在
        webDriver.findElement(By.cssSelector("body > div.register-container > div > div:nth-child(3) > span")); // 密码提示信息
        webDriver.findElement(By.cssSelector("body > div.nav")); // 导航栏
        webDriver.findElement(By.cssSelector("body > div.register-container > div > h3")); // 注册标题
    }

    /**
     * 注册成功测试
     */
    public void registerSuc() {
        // 清除输入框内文本
        webDriver.findElement(By.cssSelector("#name")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        // 输入用户名和密码
        webDriver.findElement(By.cssSelector("#name")).sendKeys("zhaoliu");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
        // 点击登录按钮
        webDriver.findElement(By.cssSelector("#submit")).click();

        // 通过页面标题检查是否登录成功
        String expect = webDriver.getTitle();
        assert expect.equals("登录");
        // 退回到注册页面
        webDriver.navigate().back();
    }

    /**
     * 注册失败 —— 账号错误 测试
     * 1. 账号为空
     * 2. 用户名已存在
     */
    public void registerNameFail() {
        // 1. 未输入账号
        // 清除输入框内文本
        webDriver.findElement(By.cssSelector("#name")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
        webDriver.findElement(By.cssSelector("#submit")).click();
        // 处理弹窗
        WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(5));
        wait.until(ExpectedConditions.alertIsPresent());// 等待弹窗出现
        Alert alert = webDriver.switchTo().alert();
        alert.accept();

        // 2. 账号已存在
        // 清除输入框内文本
        webDriver.findElement(By.cssSelector("#name")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#name")).sendKeys("zhangsan");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("123456");
        webDriver.findElement(By.cssSelector("#submit")).click();
        // 处理弹窗
        wait = new WebDriverWait(webDriver, Duration.ofSeconds(5));
        wait.until(ExpectedConditions.alertIsPresent());// 等待弹窗出现
        alert = webDriver.switchTo().alert();
        alert.accept();

    }

    /**
     * 注册失败 —— 密码错误 测试
     * 1. 密码为空
     * 2. 密码过长或过短
     * 3. 密码中包含特殊字符
     */
    public void registerPasswordFail() {
        // 1. 未输入密码
        // 清除输入框内文本
        webDriver.findElement(By.cssSelector("#name")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#name")).sendKeys("admin");
        webDriver.findElement(By.cssSelector("#submit")).click();
        // 处理弹窗
        WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(5));
        wait.until(ExpectedConditions.alertIsPresent());// 等待弹窗出现
        Alert alert = webDriver.switchTo().alert();
        alert.accept();

        // 2. 密码过长
        // 清除输入框内文本
        webDriver.findElement(By.cssSelector("#name")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#name")).sendKeys("admin");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("1234591111111");
        webDriver.findElement(By.cssSelector("#submit")).click();
        // 处理弹窗
        wait = new WebDriverWait(webDriver, Duration.ofSeconds(5));
        wait.until(ExpectedConditions.alertIsPresent());// 等待弹窗出现
        alert = webDriver.switchTo().alert();
        alert.accept();

        // 3. 密码过短
        // 清除输入框内文本
        webDriver.findElement(By.cssSelector("#name")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#name")).sendKeys("admin");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("12345");
        webDriver.findElement(By.cssSelector("#submit")).click();
        // 处理弹窗
        wait = new WebDriverWait(webDriver, Duration.ofSeconds(5));
        wait.until(ExpectedConditions.alertIsPresent());// 等待弹窗出现
        alert = webDriver.switchTo().alert();
        alert.accept();

        // 4. 密码中包含特殊字符
        // 清除输入框内文本
        webDriver.findElement(By.cssSelector("#name")).clear();
        webDriver.findElement(By.cssSelector("#password")).clear();
        webDriver.findElement(By.cssSelector("#name")).sendKeys("admin");
        webDriver.findElement(By.cssSelector("#password")).sendKeys("12345@");
        webDriver.findElement(By.cssSelector("#submit")).click();
        // 处理弹窗
        wait = new WebDriverWait(webDriver, Duration.ofSeconds(5));
        wait.until(ExpectedConditions.alertIsPresent());// 等待弹窗出现
        alert = webDriver.switchTo().alert();
        alert.accept();
    }
}
